#!/usr/bin/env bash

__rvm_set_versions_conf()
{
  \typeset gemset identifier

  if [[ -s .versions.conf ]]
  then
    \command \mv .versions.conf .versions.conf.$(__rvm_date +%m.%d.%Y-%H:%M:%S)
    rvm_warn ".version.conf is not empty, moving aside to preserve."
  fi

  identifier=$(__rvm_env_string)
  gemset=${identifier#*@}
  identifier=${identifier%@*}

  printf "%b" "ruby=$identifier
" >> .versions.conf
  if [[ -n "$gemset" && "$gemset" != "$identifier" ]]
  then
    printf "%b" "ruby-gemset=$gemset
" >> .versions.conf
  else
    printf "%b" "#ruby-gemset=my-projectit
" >> .versions.conf
  fi
  printf "%b" "#ruby-gem-install=bundler rake
#ruby-bundle-install=true
" >> .versions.conf
}

__rvm_set_ruby_version()
{
  if
    [[ -s .ruby-version ]]
  then
    \command \mv .ruby-version .ruby-version.$(__rvm_date +%m.%d.%Y-%H:%M:%S)
    rvm_warn ".ruby-version is not empty, moving aside to preserve."
  fi
  \typeset __version="$(__rvm_env_string)"
  case "${__version}" in
    (*@*)
      if
        [[ -s .ruby-gemset ]]
      then
        \command \mv .ruby-gemset .ruby-gemset.$(__rvm_date +%m.%d.%Y-%H:%M:%S)
        rvm_warn ".ruby-gemset is not empty, moving aside to preserve."
      fi
      echo "${__version##*@}" > .ruby-gemset
      ;;
    (*)
      if
        [[ -s .ruby-gemset ]]
      then
        \command \mv .ruby-gemset .ruby-gemset.$(__rvm_date +%m.%d.%Y-%H:%M:%S)
        rvm_warn ".ruby-gemset not needed, moving aside to preserve."
      fi
      ;;
  esac
  echo "${__version%@*}" > .ruby-version
}

__rvm_set_rvmrc()
{
  \typeset flags identifier short_identifier gem_file
  true ${rvm_verbose_flag:=0}

  if [[ "$HOME" != "$PWD" && "${rvm_prefix:-}" != "$PWD" ]]
  then
    if (( rvm_verbose_flag ))
    then flags="use "
    fi

    if [[ -s .rvmrc ]]
    then
      \command \mv .rvmrc .rvmrc.$(__rvm_date +%m.%d.%Y-%H:%M:%S)
      rvm_warn ".rvmrc is not empty, moving aside to preserve."
    fi

    identifier=$(__rvm_env_string)
    short_identifier="${identifier#ruby-}"
    short_identifier="${short_identifier%%-*}"

    printf "%b" "#!/usr/bin/env bash

# This is an RVM Project .rvmrc file, used to automatically load the ruby
# development environment upon cd'ing into the directory

# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
# Only full ruby name is supported here, for short names use:
#     echo \"rvm use ${short_identifier}\" > .rvmrc
environment_id=\"$identifier\"

# Uncomment the following lines if you want to verify rvm version per project
# rvmrc_rvm_version=\"${rvm_version}\" # 1.10.1 seems like a safe start
# eval \"\$(echo \${rvm_version}.\${rvmrc_rvm_version} | awk -F. '{print \"[[ \"\$1*65536+\$2*256+\$3\" -ge \"\$4*65536+\$5*256+\$6\" ]]\"}' )\" || {
#   echo \"This .rvmrc file requires at least RVM \${rvmrc_rvm_version}, aborting loading.\"
#   exit 1
# }
" >> .rvmrc
    if __rvm_string_match "$identifier" "jruby*"
    then
      printf "%b" "
# Uncomment following line if you want options to be set only for given project.
# PROJECT_JRUBY_OPTS=( --1.9 )
# The variable PROJECT_JRUBY_OPTS requires the following to be run in shell:
#    chmod +x \${rvm_path}/hooks/after_use_jruby_opts
" >> .rvmrc
    fi
    printf "%b" "
# First we attempt to load the desired environment directly from the environment
# file. This is very fast and efficient compared to running through the entire
# CLI and selector. If you want feedback on which environment was used then
# insert the word 'use' after --create as this triggers verbose mode.
if [[ -d \"\${rvm_path:-\$HOME/.rvm}/environments\"
  && -s \"\${rvm_path:-\$HOME/.rvm}/environments/\$environment_id\" ]]
then
  \\. \"\${rvm_path:-\$HOME/.rvm}/environments/\$environment_id\"
  for __hook in \"\${rvm_path:-\$HOME/.rvm}/hooks/after_use\"*
  do
    if [[ -f \"\${__hook}\" && -x \"\${__hook}\" && -s \"\${__hook}\" ]]
    then \\. \"\${__hook}\" || true
    fi
  done
  unset __hook
" >> .rvmrc
    if [[ " $flags " == *" use "* ]]
    then printf "%b" "  if (( \${rvm_use_flag:=1} >= 1 )) # display automatically"    >> .rvmrc
    else printf "%b" "  if (( \${rvm_use_flag:=1} >= 2 )) # display only when forced" >> .rvmrc
    fi
    printf "%b" "
  then
    if [[ \$- == *i* ]] # check for interactive shells
    then printf \"%b\" \"Using: \$(tput setaf 2 2>/dev/null)\$GEM_HOME\$(tput sgr0 2>/dev/null)\\\\n\" # show the user the ruby and gemset they are using in green
    else printf \"%b\" \"Using: \$GEM_HOME\\\\n\" # don't use colors in non-interactive shells
    fi
  fi
" >> .rvmrc
    printf "%b" "else
  # If the environment file has not yet been created, use the RVM CLI to select.
  rvm --create $flags \"\$environment_id\" || {
    echo \"Failed to create RVM environment '\${environment_id}'.\"
    return 1
  }
fi
" >> .rvmrc
    for gem_file in *.gems
    do
      case "$gem_file" in
        (\*.gems) continue ;;
      esac
      printf "%b" "
# If you use an RVM gemset file to install a list of gems (*.gems), you can have
# it be automatically loaded. Uncomment the following and adjust the filename if
# necessary.
#
# filename=\".gems\"
# if [[ -s \"\$filename\" ]]
# then
#   rvm gemset import \"\$filename\" | GREP_OPTIONS=\"\" \\\\command \\grep -v already | GREP_OPTIONS=\"\" \command \grep -v listed | GREP_OPTIONS=\"\" \command \grep -v complete | \command \sed '/^$/d'
# fi
" >> .rvmrc
    done
    if [[ -s Gemfile ]]
    then
      printf "%b" "
# If you use bundler, this might be useful to you:
# if [[ -s Gemfile ]] && {
#   ! builtin command -v bundle >/dev/null ||
#   builtin command -v bundle | GREP_OPTIONS=\"\" \\\\command \\grep \$rvm_path/bin/bundle >/dev/null
# }
# then
#   printf \"%b\" \"The rubygem 'bundler' is not installed. Installing it now.\\\\n\"
#   gem install bundler
# fi
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
# then
#   bundle install | GREP_OPTIONS=\"\" \\\\command \\grep -vE '^Using|Your bundle is complete'
# fi
" >> .rvmrc
    fi
  else
    rvm_error ".rvmrc cannot be set in your home directory.\
      \nThe home .rvmrc is for global rvm settings only."
  fi
}
